package com.erp.module.packPiece

import android.content.Context
import android.util.Log
import com.erp.bean.PackPieceBean
import com.erp.Interface.OnUserDataInterface
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.contacts.GlobalData.gonghaos
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2018/12/14/014.
 * 作者：刘洋
 */
class PackPieceP : PackPieceContract.PackPieceKP {

    var mlist : MutableList<PackPieceBean>
    var PV : PackPieceContract.PackPiecePutView?=null
    var FV : PackPieceContract.PackPieceFragmentView?=null
    var page = 0
    var pagesize = 20
    var MP : PackPieceContract.PackPieceMP = PackPieceM()
    var context: Context
    var sousuo = FragmentPackPiece.SousuoBean()
    var name =""
    var counts =""
    var gangwei_name=""

    constructor(mlist: MutableList<PackPieceBean>, PV: PackPieceContract.PackPiecePutView, context: Context) {
        this.mlist = mlist
        this.PV = PV
        this.context=context
    }
    constructor(mlist: MutableList<PackPieceBean>, FV: PackPieceContract.PackPieceFragmentView, context: Context) {
        this.mlist = mlist
        this.FV = FV
        this.context=context
    }
    override fun getPackPiece(sousuo : FragmentPackPiece.SousuoBean) {
        this.sousuo=sousuo
        page=0
        MP.getPackPiece(context,page,pagesize,sousuo,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                if(data==null){
                    FV!!.getPackPieceNo()
                    return
                }
                Log.d("PackPiece",data)
                mlist.clear()
                getJosn(data)
                FV!!.getPackPieceOk(name,counts,gangwei_name)
            }

            override fun onNot(throwable: String?) {
                FV!!.getPackPieceNo()
                Log.d("PackPiece",throwable.toString())
            }

        })
    }

    override fun addPackPiece() {
        page++
        MP.getPackPiece(context,page,pagesize,sousuo,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                if(data==null){
                    FV!!.getPackPieceOk(name,counts,gangwei_name)
                    return
                }
                Log.d("PackPiece",data)
                getJosn(data)
                FV!!.addPackPieceOk()
            }

            override fun onNot(throwable: String?) {
                FV!!.addPackPieceNo()
                Log.d("PackPiece",throwable.toString())
            }

        })
    }

    fun getJosn(data :String){
        try {
            val jom = JSONObject(data)
            val js = JsonUtil.getjsvolue(jom,"index")
            for (i in 0 until js.length()){
                val bean = PackPieceBean()
                val jo = js[i] as JSONObject
                bean.gangwei_name=JsonUtil.getvolue(jo,"gangwei_name")
                bean.batch_name=JsonUtil.getvolue(jo,"batch_name")
                bean.cnum=JsonUtil.getvolue(jo,"cnum")
//                bean.mc1_mark=JsonUtil.getvolue(jo,"mc1_mark")
                bean.count=JsonUtil.getvolue(jo,"count")
                bean.ctime=JsonUtil.getvolue(jo,"ctime")
                bean.work_staff=JsonUtil.getvolue(jo,"work_staff")
                mlist.add(bean)
            }
            if(page==0) {
                val jot = JsonUtil.getjovolue(jom, "tongji")
                name=JsonUtil.getvolue(jot,"name")
                counts=JsonUtil.getvolue(jot,"counts")
                gangwei_name=JsonUtil.getvolue(jot,"gangwei_name")
            }

        }catch (e : JSONException){
            e.printStackTrace()
        }
    }

    override fun putPackPiece(work_staff_id : String) {
        MP.putPackPiece(context,work_staff_id,mlist,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                PV!!.putPackPieceOk()
                Log.d("PackPiece",data)
            }

            override fun onNot(throwable: String?) {
                PV!!.putPackPieceNo()
                Log.d("PackPiece",throwable.toString())
            }

        })
    }

    override fun getGonghao() {
        if(gonghaos=="") {
            Httputil.getZhiLiangGongHaoLieBiao(context, object : OnUserDataInterface {
                override fun onUserData(data: String) {
                    Myutil.MyTost("获取工号列表")
                    gonghaos = data
                    getgonghaojson(data)
                }

                override fun onNot(throwable: String?) {
                    Log.d("TAAAG", "throwable=" + Myutil.decodeUnicode(throwable))
                    if (throwable != null) {
                        if(PV!=null) {
                            PV!!.getGonghaoNO()
                        }
                        if(FV!=null) {
                            FV!!.getGonghaoNO()
                        }
                    }
                }

            })
        }else{
            getgonghaojson(gonghaos)
        }
    }

    fun getgonghaojson(data: String){
        var gonghao: MutableList<String> = ArrayList()
        var gonghaoname: MutableList<String> = ArrayList()
        try {
            val jo = JSONObject(data)
            val keys = jo.keys()
            while (keys.hasNext()) {
                val key = keys.next() as String
                if(key!="0") {
                    gonghao.add(key)
                    gonghaoname.add(JsonUtil.getvolue(jo, key))
                }
            }
            if(PV!=null) {
                PV!!.getGonghaoOK(gonghao, gonghaoname)
            }
            if(FV!=null) {
                FV!!.getGonghaoOK(gonghao, gonghaoname)
            }
        } catch (e: Exception) {
            Log.d("TAAAG", "e=" + e.toString())
            e.printStackTrace()
        }
    }

}